SpringMVC框架(2) —— 参数绑定

项目结构

  • java
    • controller
      • ParameterController.java(Java文件)
    • utils
      • StringToData(自定义类型转换文件)
    • domain
      • User(JavaBean本件)
      • Account(JavaBean本件)
      • CCollection(JavaBean本件)
      • DDate(JavaBean本件)
  • resources
    • springmvc.xml(springmvc配置文件)
  • webapp
    • WEB-INF
      • pages
        • success.jsp(web页面)
      • web.xml(web配置文件)
    • index.jsp(web页面)
    • parameters.jsp(web页面)
  • pom.xml(maven项目配置文件)

maven项目配置文件

  • 配置maven项目需要的依赖
    • spring-context
    • spring-web
    • spring-webmvc
    • servlet-api
    • jsp-api

web配置文件

  • 前端控制器(DispatcherServlet)
    • 配置一个前端控制器(<servlet>)
      • 创建前端控制器时,加载Spring配置文件(<init-param>)
      • 启动服务器时,创建前端控制器(<\load-on-startup>)
    • 配置前端控制器的作用范围(<servlet-mapping>)
  • 过滤器(CharacterEncodingFilter)
    • 配置一个过滤器(<filter>)
      • 配置编码(<init-param>):解决中文乱码
    • 配置前端控制器的作用范围(<filter-mapping>)

springmvc配置文件

  • 导入名称空间(<beans xmlns=””>)
  • 开启注解扫描(<context:component-scan base-package=””>)
  • 配置视图解析器(InternalResourceViewResolver)
    • 配置前缀(prefix -> “/WEB-INF/pages”)
    • 配置后缀(suffix -> “.jsp”)
  • 配置类型转换器(ConversionServiceFactoryBean)
    • 注册自定义类型转换器
  • 开启SpringMVC注解支持(<mvc:annotation-driven>)

Java文件

  • 表现层
      • 添加进IoC核心容器(@Controller)
      • 设置请求映射(@RequestMapping())
        • 一级目录(path=””)
        • 设置允许访问的请求方法(method=””)
        • 设置必需的参数和参数值(params=””)
        • 设置必需的请求头(headers=””)
    • 方法
      • 设置请求映射(@RequestMapping())
        • 二级目录(path=””)
      • 返回值(return “success”;)
      • 参数列表(数据类型 请求参数名,数据类型 请求参数名)

JSP文件

  • 超链接(href=”一级目录/二级目录?请求参数=值&请求参数=值”)

  • 表单(<form action=”一级目录/二级目录” method=”post”>)

执行代码

  • 需求:在parameters.jsp页面,点击的超链接,跳转到success.jsp页面,并且在控制台输出请求参数

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.water</groupId>
<artifactId>section01_Introduction</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>section01_Introduction Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<!-- 版本锁定 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>

<!-- 依赖注入 -->
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- jsp -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>section01_Introduction</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- 【Filter】 -->
<filter>
<!-- 创建过滤器 -->
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 配置编码 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 映射 -->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 【Servlet】 -->
<servlet>
<!-- 创建前端控制器 -->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 创建前端控制器时,加载spring配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 启动服务器时,创建前端控制器 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 映射 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

springmvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">


<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.water"/>

<!-- 视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 类型转换器 -->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<!-- Configure the set of custom converter objects that should be added -->
<property name="converters">
<!-- Set集合 -->
<set>
<!-- 添加自定义实现类 -->
<bean class="cn.water.utils.StringToDate"></bean>
</set>
</property>
</bean>

<!-- 开启springMVC框架注解支持 -->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>


</beans>

StringToDate.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cn.water.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author Water
* @date 2019/9/10 - 8:53
*/
public class StringToDate implements Converter<String, Date> {


/**
*
* @param source
* @return
*/
@Override
public Date convert(String source) {

// 判断输入数据是否为空
if (source == null){
throw new RuntimeException("请您输入日期");
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

// 转换格式
try {
return dateFormat.parse(source);
} catch (Exception e) {
System.out.println("请您输入正确的日期格式(yyyy-MM-dd)");
throw new RuntimeException("请您输入正确的日期格式(yyyy-MM-dd)");
}

}
}

ParameterController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cn.water.controller;

import cn.water.domain.Account;
import cn.water.domain.CCollection;
import cn.water.domain.DDate;
import cn.water.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/parameter")
public class parameterController {

/* 获取数据 */
@RequestMapping("/testParameter")
public String testParameter(String cat,String dog,String rat){
System.out.println("1、方法执行了");
System.out.println("2、获取参数:"+cat+dog+rat);
return "success";
}

/* 获取数据,并封装至JavaBean(User)中。 */
@RequestMapping("/testJavaBean")
public String testJavaBean(User user){
System.out.println(user.toString() );
return "success";
}

/* 获取数据,并封装至JavaBean中,且此JavaBean(Account)含有其他JavaBean对象(User)。 */
@RequestMapping("/testJavaBean222")
public String testJavaBean2(Account account){
System.out.println(account.toString() );
return "success";
}

/* 获取数据,并封装至JavaBean中,且此JavaBean(Account)含有List集合、Map集合。 */
@RequestMapping("/testCollection")
public String testCollection(CCollection cCollection){
System.out.println(cCollection);
return "success";
}

/* 获取字符串数据,自动转换为日期类型数据(Spring框架自动转换器) */
@RequestMapping("/testDate")
public String testDate(DDate date){
System.out.println("Date:"+date);
return "success";
}

/* 获取Servlet原生API */
@RequestMapping("/testServlet")
public String testServlet(HttpServletRequest request, HttpServletResponse response){
// Request对象
System.out.println("【Request】"+request);
// Response对象
System.out.println("【Response】"+response);
// Session对象
HttpSession session = request.getSession();
System.out.println("【Session】"+session);
// ServletContext对象
ServletContext servletContext = session.getServletContext();
System.out.println("【ServletContext】"+servletContext);

return "success";
}

}

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2><a href="controller/sayHello?username=cat">入门</a></h2>
<hr>
<h2><a href="parameters.jsp">参数绑定</a></h2>
<hr>
<h2><a href="annotation.jsp">注解</a></h2>
<hr>
</body>
</html>

parameters.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 普通类型 --%>
<h3><a href="parameter/testParameter?cat=11&dog=22&rat=33">封装至 普通数据类型</a></h3>
<hr>

<%-- 封装至JavaBean --%>
<form action="parameter/testJavaBean" method="post"> <%-- post请求方式中文会乱码,get请求方式不会 --%>
姓名:<input type="text" name="name" /><br/>
年龄:<input type="text" name="age" /><br/>
<input type="submit" value="封装至 JavaBean类型">
</form>
<hr>

<%-- 封装至JavaBean,含有JavaBean --%>
<form action="parameter/testJavaBean222" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="text" name="password" /><br/>
金额:<input type="text" name="money" /><br/>
用户姓名:<input type="text" name="user.name" /><br/>
用户年龄:<input type="text" name="user.age" /><br/>
<input type="submit" value="封装至 JavaBean类型 含有JavaBean">
</form>
<hr>

<%-- 封装至JavaBean,含有集合 --%>
<form action="parameter/testCollection" method="post">
<%-- List<User> list[0]==User --%>
用户姓名:<input type="text" name="list[0].name" /><br/>
用户年龄:<input type="text" name="list[0].age" /><br/>
<%-- Map<String,User> 'one'==String --%>
用户姓名:<input type="text" name="map['one'].name" /><br/>
用户年龄:<input type="text" name="map['one'].age" /><br/>
<input type="submit" value="封装至 JavaBean类型 含有List集合和Map集合">
</form>
<hr>

<%-- 封装至JavaBean,含有Data类型 --%>
<%-- 网页中input标签的Test属性的文本框中输入数据类型一定是字符串形式 --%>
<%-- 默认配置下,String框架自动将请求的字符串格式自动转换为JavaBean中对应的格式,但是对于文本格式有所要求,例如日期 --%>
<%-- 默认正确日期格式:2019/01/01 --%>
<%-- 自定义正确日期格式:2019-01-01 --%>
<form action="parameter/testDate" method="post">
日期:<input type="text" name="date" /><br/>
<input type="submit" value="封装至 日期类型">
</form>
<hr>


</body>
</html>

success.jsp

1
2
3
4
5
6
7
8
9
10
11
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>

<h3>访问成功!!!</h3>

</body>
</html>

获取请求数据

封装至 普通数据类型

普通数据类型

  • 发送请求

    1
    <a href="parameter/testParameter?cat=11&dog=22&rat=33">封装至 普通数据类型</a>
  • 接收请求

    1
    2
    3
    4
    5
    6
    @RequestMapping("/testParameter")
    public String testParameter(String cat,String dog,String rat){
    System.out.println("1、方法执行了");
    System.out.println("2、获取参数:"+cat+dog+rat);
    return "success";
    }
  • 运行结果

    1
    2
    1、方法执行了
    2、获取参数:112233

封装至 JavaBean类型

JavaBean类型

  • 数据类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    package cn.water.domain;

    import java.io.Serializable;

    public class User implements Serializable {

    private String name;
    private String age;

    @Override
    public String toString() {
    return "User{" +
    "name='" + name + '\'' +
    ", age='" + age + '\'' +
    '}';
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getAge() {
    return age;
    }

    public void setAge(String age) {
    this.age = age;
    }
    }
  • 发送请求

    1
    2
    3
    4
    5
    6
    <form action="parameter/testJavaBean" method="post"> 
    <%-- post请求方式中文会乱码,get请求方式不会 --%>
    姓名:<input type="text" name="name" /><br/>
    年龄:<input type="text" name="age" /><br/>
    <input type="submit" value="封装至 JavaBean类型">
    </form>
  • 接收请求

    1
    2
    3
    4
    5
    @RequestMapping("/testJavaBean")
    public String testJavaBean(User user){
    System.out.println(user.toString() );
    return "success";
    }
  • 运行结果

    1
    User{name='cat', age='11'}

含有JavaBean

  • 数据类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    package cn.water.domain;

    import java.io.Serializable;

    public class Account implements Serializable {

    private String username;
    private String password;
    private Double money;
    private User user;

    public Double getMoney() {
    return money;
    }

    public void setMoney(Double money) {
    this.money = money;
    }

    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username = username;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public User getUser() {
    return user;
    }

    public void setUser(User user) {
    this.user = user;
    }

    @Override
    public String toString() {
    return "Account{" +
    "username='" + username + '\'' +
    ", password='" + password + '\'' +
    ", money=" + money +
    ", user=" + user +
    '}';
    }

    }
  • 发送请求

    1
    2
    3
    4
    5
    6
    7
    8
    <form action="parameter/testJavaBean222" method="post">
    姓名:<input type="text" name="username" /><br/>
    密码:<input type="text" name="password" /><br/>
    金额:<input type="text" name="money" /><br/>
    姓名:<input type="text" name="user.name" /><br/>
    年龄:<input type="text" name="user.age" /><br/>
    <input type="submit" value="封装至 JavaBean类型 含有JavaBean">
    </form>
  • 接收请求

    1
    2
    3
    4
    5
    @RequestMapping("/testJavaBean222")
    public String testJavaBean2(Account account){
    System.out.println(account.toString() );
    return "success";
    }
  • 运行结果

    1
    Account{username='cat', password='123695', money=999.0, user=User{name='cat', age='11'}}

含有List集合和Map集合

  • 数据类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package cn.water.domain;

    import java.io.Serializable;
    import java.util.List;
    import java.util.Map;

    public class CCollection implements Serializable {

    private List<User> list;
    private Map<String,User> map;

    @Override
    public String toString() {
    return "CCollection{" +
    "list=" + list +
    ", map=" + map +
    '}';
    }

    public List<User> getList() {
    return list;
    }

    public void setList(List<User> list) {
    this.list = list;
    }

    public Map<String, User> getMap() {
    return map;
    }

    public void setMap(Map<String, User> map) {
    this.map = map;
    }
    }
  • 发送请求

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <form action="parameter/testCollection" method="post">
    <%-- List<User> list[0]==User --%>
    用户姓名:<input type="text" name="list[0].name" /><br/>
    用户年龄:<input type="text" name="list[0].age" /><br/>
    <%-- Map<String,User> 'one'==String --%>
    用户姓名:<input type="text" name="map['one'].name" /><br/>
    用户年龄:<input type="text" name="map['one'].age" /><br/>
    <input type="submit" value="封装至 JavaBean类型 含有List集合和Map集合">
    </form>
  • 接收请求

    1
    2
    3
    4
    5
    @RequestMapping("/testCollection")
    public String testCollection(CCollection cCollection){
    System.out.println(cCollection);
    return "success";
    }
  • 运行结果

    1
    CCollection{list=[User{name='cat', age='11'}], map={one=User{name='dog', age='22'}}}

封装至 日期类型

日期类型

  • 数据类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    package cn.water.domain;

    import java.io.Serializable;
    import java.util.Date;

    public class DDate implements Serializable {

    private Date date;


    public Date getDate() {
    return date;
    }

    public void setDate(Date date) {
    this.date = date;
    }

    @Override
    public String toString() {
    return "DDate{" +
    "date=" + date +
    '}';
    }
    }
  • 自定义类型转换器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package cn.water.utils;

    import org.springframework.core.convert.converter.Converter;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class StringToDate implements Converter<String, Date> {

    @Override
    public Date convert(String source) {

    // 判断输入数据是否为空
    if (source == null){
    throw new RuntimeException("请您输入日期");
    }
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    // 转换格式
    try {
    return dateFormat.parse(source);
    } catch (Exception e) {
    throw new RuntimeException("请您输入正确的日期格式(yyyy-MM-dd)");
    }

    }
    }
  • 发送请求

    1
    2
    3
    4
    5
    6
    7
    8
    <%-- 网页中input标签的Test属性的文本框中输入数据类型一定是字符串形式 --%>
    <%-- 默认配置下,String框架自动将请求的字符串格式自动转换为JavaBean中对应的格式,但是对于文本格式有所要求,例如日期 --%>
    <%-- 正确日期格式:2019/01/01 --%>
    <%-- 错误日期格式:2019-01-01; 20190101 --%>
    <form action="parameter/testDate" method="post">
    日期:<input type="text" name="date" /><br/>
    <input type="submit" value="封装至 日期类型">
    </form>
  • 接收请求

    1
    2
    3
    4
    5
    @RequestMapping("/testDate")
    public String testDate(DDate date){
    System.out.println("Date:"+date);
    return "success";
    }
  • 运行结果

    1
    2
    输入:2011-9-1 10:25:45
    结果:Date:DDate{date=Thu Sep 01 10:25:45 CST 2011}

获取Servlet原生API

Request

Response

Session

ServletContext

  • 发送请求

    1
    <a href="parameter/testServlet">请点击</a>
  • 接收请求

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
        @RequestMapping("/testServlet")
    public String testServlet(HttpServletRequest request, HttpServletResponse response){
    // Request对象
    System.out.println("【Request】"+request);
    // Response对象
    System.out.println("【Response】"+response);
    // Session对象
    HttpSession session = request.getSession();
    System.out.println("【Session】"+session);
    // ServletContext对象
    ServletContext servletContext = session.getServletContext();
    System.out.println("【ServletContext】"+servletContext);

    return "success";
    }
  • 运行结果

    1
    2
    3
    4
    【Request】org.apache.catalina.connector.RequestFacade@736f0cc
    【Response】org.apache.catalina.connector.ResponseFacade@3755e816
    【Session】org.apache.catalina.session.StandardSessionFacade@56651c25
    【ServletContext】org.apache.catalina.core.ApplicationContextFacade@4dc6868
-------------本文结束-------------
Donate comment here